package ejb;

import java.util.List;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import model.Notification;
import model.User;

@MessageDriven(
		activationConfig = { 
				@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
				@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/test")
		})
public class ListenNotification extends FacadeBean implements MessageListener {

	public ListenNotification(){
	}

	@Override
	public void onMessage(Message message) {
		ObjectMessage msg = (ObjectMessage) message;
		System.out.println("Ecouteur MDB");
		try{
			Notification notification = (Notification) msg.getObject();
			Notification n = getNotification(notification.getIdNotification());
			User u = getUser(n.getIdReceveur());
			List<Notification> listeNotification = u.getNotification();
			listeNotification.add(n);
			u.setNotification(listeNotification);
		} catch(JMSException e){
			e.printStackTrace();
		}
	}
}
